package com.dt76.smallloanbackenduser.controller.etao;

import com.dt76.smallloanbackenduser.pojo.etao.CurrentUser;
import com.dt76.smallloanbackenduser.pojo.etao.Employee;
import com.dt76.smallloanbackenduser.pojo.etao.LoginLog;
import com.dt76.smallloanbackenduser.remote.etao.EmployeeRemote;
import com.dt76.smallloanbackenduser.remote.etao.LoginRemote;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private LoginRemote loginRemote;
    @Autowired
    private EmployeeRemote employeeRemote;

    //发送邮件注入
    @Autowired
    private JavaMailSenderImpl mailSender;



    //登出
    @RequestMapping("/loginout")
    public String loginout(HttpSession session) {
        session.removeAttribute("sessionEmp");
        return "login";
    }

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public String doLogin(String empLoginName, String empPasswrod, Model model,HttpSession session) {

//        登录入口,进行shiro校验登录
        if (empLoginName != null && empPasswrod != null) {
            CurrentUser currentUser = loginRemote.doLogin(empLoginName, empPasswrod);
            //调用远程方法进行认证
            if (currentUser.getAuthenticated()) {
                System.out.println("登录信息----" + currentUser.getLoginMsg());
                System.out.println("角色是手机号----" + currentUser.getcEmp().getPhone());
                System.out.println("角色是姓名----" + currentUser.getcEmp().getEmpName());
                //将登录人信息插入到登录日志表中
                //手机号.姓名,当前时间
                LoginLog loginLog = new LoginLog(currentUser.getcEmp().getEmpName(),
                        currentUser.getcEmp().getEmpLoginName(),
                        currentUser.getcEmp().getPhone());
                Result result = loginRemote.addLoginMsg(loginLog);
                System.out.println("是否添加日志成功-----" + result.isSuccess());
                System.out.println("角色是----" + currentUser.getcEmp().getRoleStr());
                System.out.println("权限菜单----" + currentUser.getcEmp().getMeuns().toString());
                model.addAttribute("menus", currentUser.getcEmp().getMeuns());
                model.addAttribute("role", currentUser.getcEmp().getRoleStr());
                session.setAttribute("sessionEmp",currentUser.getcEmp());
                return "index";
            } else {
                model.addAttribute("loginMsg", currentUser.getLoginMsg());
                return "login";
            }
        }
        model.addAttribute("loginMsg", "用户名或密码不能为空");
        return "login";
    }



    @RequestMapping(value = "/forgetPwd", method = RequestMethod.POST)
    public String findPWD(String empLoginName,Model model){
        System.out.println(empLoginName+"----忘记了密码");
        Employee employee = employeeRemote.getEmpLoginName(empLoginName);
        if (employee == null) {
            model.addAttribute("msg", "您输入的用户不存在");
        } else {
//        需要调发送邮件接口,给指定邮箱地址发送邮件找回密码
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("找回密码");
            message.setText("您的密码是<"+employee.getEmpPasswrod()+">");
            message.setTo(employee.getEmail());
            message.setFrom("913753579@qq.com");
            mailSender.send(message);
            model.addAttribute("empLoginName",empLoginName );
            model.addAttribute("msg", "已发送邮件至您的邮箱");
        }
        return "login";
    }
}
